<template>
  <el-container class="index">
    <el-header>
      <div class="logo">
        <img src="../assets/logo.png" alt />
      </div>
      <div class="title">
        <h1>电商管理系统</h1>
      </div>
      <div class="logout">
        欢迎光临~
        <a href="JavaScript:;" @click="logout">退出</a>
      </div>
    </el-header>
    <el-container>
      <el-aside width="200px">
        <img src="../assets/小吴.jpg" alt="">
        <el-menu :default-active="defaultActive" class="el-menu-vertical-demo" background-color="#545c64" text-color="#fff" active-text-color="#ffd04b" router unique-opened>
          <el-submenu :index="item.path" v-for='item in asideList' :key='item.id'>
            <!-- 标题 -->
            <template v-slot:title>
              <i class="el-icon-location"></i>
              <span>{{item.authName}}</span>
            </template>

            <el-menu-item :index="child.path" v-for='child in item.children' :key='child.id'>
              <i class="el-icon-menu"></i>
              <span slot="title">{{child.authName}}</span>
            </el-menu-item>
          </el-submenu>

        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      asideList: []
    }
  },
  methods: {
    // 退出登录
    logout () {
      this.$confirm('亲,你确定要退出管理系统吗', '温馨提示', {
        type: 'warning',
        center: true
      })
        .then(() => {
          this.$message.success('成功退出')
          localStorage.removeItem('token')
          this.$router.push('/login')
        })
        .catch(() => { })
    }

  },
  computed: {
    defaultActive () {
      return this.$route.path.substring(1).split('-')[0]
    }
  },
  async created () {
    const { meta, data } = await this.$axios.get('menus')
    if (meta.status === 200) {
      this.asideList = data
    }
  }
}
</script>

<style lang = 'scss' scoped>
.index {
  height: 100%;
  .el-header {
    background-color: #d8d8d8;
    display: flex;
    .logo {
      width: 180px;
      img {
        height: 40px;
        margin: 10px;
      }
    }
    .title {
      flex: 1;
      h1 {
        text-align: center;
        height: 60px;
        line-height: 60px;
        color: #545c64;
      }
    }
    .logout {
      height: 60px;
      line-height: 60px;
      text-align: right;
      font-weight: 700;
      width: 180px;
      a {
        color: orange;
      }
    }
  }
  .el-aside {
    background-color: #545c64;
    img {
      width: 120px;
      height: 120px;
      display: block;
      border-radius: 50%;
      margin: 30px auto 10px;
    }
  }
  .el-main {
    background-color: #ecf0f1;
  }
}
</style>
